package com.xiegk.ticket;

import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 单应用同步锁
 * - ReentrantLock
 *
 * @author xgk
 * @date 2022/2/20
 */
@Component
public class TicketReeLock02 {
    private int number = 50;
    private final Lock lock = new ReentrantLock();

    // juc - ReentrantLock 锁
    public void sale() {
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "\t" + "卖出第" + (number--) + "\t还剩下：" + number);
            }
        } finally {
            lock.unlock();
        }
    }

}
